package com.huawei.fastengine.fastview;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.RemoteException;
import com.huawei.fastengine.R;
import com.huawei.fastengine.fastview.download.download.DownloadActivity;
import com.huawei.fastengine.fastview.download.utils.CheckUtils;
import com.huawei.fastengine.fastview.download.utils.PackageUtils;
import com.huawei.fastengine.fastview.download.utils.log.FastViewLogUtils;
import com.huawei.fastengine.fastview.startFastappEngine.JumpActivity;
import com.huawei.fastengine.fastview.startFastappEngine.bean.Options;
import com.huawei.fastengine.fastview.startFastappEngine.bean.RpkPageInfo;
import o.eye;
import o.eye.d;

public class OpenFastAppEngine {
    public static final String ACTION_AIDL_FASTAPP = "com.huawei.fastapp.engine.action.AIDL_SERVICE";
    private static final String TAG = "OpenFastAppEngine";
    private static OpenFastAppEngine mInstance = new OpenFastAppEngine();
    private ServiceConnection conn = new ServiceConnection() {
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            boolean z = true;
            Message obtain = Message.obtain(OpenFastAppEngine.this.mHandler);
            obtain.what = 1002;
            OpenFastAppEngine.this.mHandler.sendMessage(obtain);
            OpenFastAppEngine.this.fastappService = d.ˎ(iBinder);
            try {
                FastViewLogUtils.d(OpenFastAppEngine.TAG, " onServiceConnected bind success");
                if (OpenFastAppEngine.this.mRpkinfo != null) {
                    String pageParam = OpenFastAppEngine.this.mRpkinfo.getPageParam();
                    if (!FastSDKEngine.canUseRpkPageNew(OpenFastAppEngine.getInstance().mContext, Config.FASTAPP_PACKAGE_NAME)) {
                        if (OpenFastAppEngine.this.mRpkinfo.isCreateShortCut() != 1) {
                            z = false;
                        }
                        OpenFastAppEngine.this.fastappService.ˎ(OpenFastAppEngine.this.mRpkinfo.getPackageName(), OpenFastAppEngine.this.mRpkinfo.getPageUri(), pageParam, OpenFastAppEngine.this.mRpkinfo.isLatestNeed(), z, null, OpenFastAppEngine.this.mRpkinfo.getSourcePackage(), OpenFastAppEngine.this.mRpkinfo.getAppName(), OpenFastAppEngine.this.mRpkinfo.getInterruptedUri());
                    } else if (FastSDKEngine.supportUserExp(OpenFastAppEngine.getInstance().mContext, Config.FASTAPP_PACKAGE_NAME)) {
                        if (OpenFastAppEngine.this.mRpkinfo.getOptions() == null) {
                            OpenFastAppEngine.this.mRpkinfo.setOptions(new Options());
                        }
                        OpenFastAppEngine.this.fastappService.ˋ(OpenFastAppEngine.this.mRpkinfo.getPackageName(), OpenFastAppEngine.this.mRpkinfo.getPageUri(), pageParam, OpenFastAppEngine.this.mRpkinfo.isLatestNeed(), OpenFastAppEngine.this.mRpkinfo.isCreateShortCut(), null, OpenFastAppEngine.this.mRpkinfo.getSourcePackage(), OpenFastAppEngine.this.mRpkinfo.getAppName(), OpenFastAppEngine.this.mRpkinfo.getInterruptedUri(), null, OpenFastAppEngine.this.mRpkinfo.getOptions().getActionbarLockCustomize(), OpenFastAppEngine.this.mRpkinfo.getOptions().isActionbarLockStatus());
                    } else {
                        OpenFastAppEngine.this.fastappService.ˋ(OpenFastAppEngine.this.mRpkinfo.getPackageName(), OpenFastAppEngine.this.mRpkinfo.getPageUri(), pageParam, OpenFastAppEngine.this.mRpkinfo.isLatestNeed(), OpenFastAppEngine.this.mRpkinfo.isCreateShortCut(), null, OpenFastAppEngine.this.mRpkinfo.getSourcePackage(), OpenFastAppEngine.this.mRpkinfo.getAppName(), OpenFastAppEngine.this.mRpkinfo.getInterruptedUri());
                    }
                }
                OpenFastAppEngine.this.unbindService();
            } catch (RemoteException e) {
                FastViewLogUtils.d(OpenFastAppEngine.TAG, " ERROR RemoteException");
            }
        }

        public void onServiceDisconnected(ComponentName componentName) {
            FastViewLogUtils.e(OpenFastAppEngine.TAG, "onServiceDisconnected");
            OpenFastAppEngine.this.fastappService = null;
        }
    };
    private AlertDialog dialog;
    private eye fastappService;
    private boolean isNeedStartActivity = false;
    private Context mContext;
    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        public void handleMessage(Message message) {
            FastViewLogUtils.e(OpenFastAppEngine.TAG, "handleMessage: " + message.what);
            Intent intent;
            switch (message.what) {
                case 1001:
                    Value value = (Value) message.obj;
                    OpenFastAppEngine.this.mRpkinfo = value.mRpkInfoValue;
                    OpenFastAppEngine.this.mContext = value.mContext;
                    if (PackageUtils.isEngineInstalled(value.mContext)) {
                        OpenFastAppEngine.this.bindService(value.mContext);
                        return;
                    } else if (OpenFastAppEngine.this.mNeedDownload) {
                        FastViewLogUtils.i(OpenFastAppEngine.TAG, "download engine");
                        intent = new Intent(OpenFastAppEngine.this.mContext, DownloadActivity.class);
                        if (OpenFastAppEngine.this.mRpkinfo != null) {
                            intent.putExtra(DownloadActivity.INTENT_BUNDLE_KEY_RPK_LOADER_PAGE, OpenFastAppEngine.this.mRpkinfo);
                        }
                        OpenFastAppEngine.this.mContext.startActivity(intent);
                        return;
                    } else {
                        return;
                    }
                case 1002:
                    return;
                case 1003:
                    FastViewLogUtils.e(OpenFastAppEngine.TAG, "BIND_FAILED: startActivity");
                    if (OpenFastAppEngine.this.mContext != null) {
                        intent = new Intent();
                        intent.setClass(OpenFastAppEngine.this.mContext, JumpActivity.class);
                        intent.putExtra("isfromold", false);
                        if (!(OpenFastAppEngine.this.mContext instanceof Activity)) {
                            intent.setFlags(268435456);
                        }
                        try {
                            OpenFastAppEngine.this.mContext.startActivity(intent);
                            if (OpenFastAppEngine.this.mContext instanceof Activity) {
                                ((Activity) OpenFastAppEngine.this.mContext).overridePendingTransition(0, 0);
                                return;
                            }
                            return;
                        } catch (ActivityNotFoundException e) {
                            FastViewLogUtils.e(OpenFastAppEngine.TAG, "ActivityNotFoundException：");
                            return;
                        }
                    }
                    return;
                case 1004:
                    OpenFastAppEngine.this.mTimeOut = false;
                    return;
                default:
                    FastViewLogUtils.e(OpenFastAppEngine.TAG, "error default ");
                    return;
            }
        }
    };
    private boolean mNeedDownload = false;
    private RpkPageInfo mRpkinfo;
    private boolean mTimeOut = false;

    static class Value {
        public Context mContext;
        public RpkPageInfo mRpkInfoValue;

        public Value(Context context, RpkPageInfo rpkPageInfo) {
            this.mContext = context;
            this.mRpkInfoValue = rpkPageInfo;
        }
    }

    private OpenFastAppEngine() {
    }

    private void bindService(Context context) {
        FastViewLogUtils.d(TAG, "begin to bindService");
        if (context == null) {
            FastViewLogUtils.e(TAG, "bindService context is null");
        } else if (!CheckUtils.isCorrectSign(context, Config.FASTAPP_PACKAGE_NAME)) {
            FastViewLogUtils.e(TAG, "FastAPP sign is Wrong");
        } else if (this.isNeedStartActivity) {
            this.isNeedStartActivity = false;
            FastViewLogUtils.e(TAG, "firstStart");
            r0 = Message.obtain(this.mHandler);
            r0.what = 1003;
            this.mHandler.sendMessage(r0);
        } else {
            Intent intent = new Intent();
            intent.setAction("com.huawei.fastapp.engine.action.AIDL_SERVICE");
            intent.setPackage(Config.FASTAPP_PACKAGE_NAME);
            if (!context.bindService(intent, this.conn, 1)) {
                FastViewLogUtils.e(TAG, "bind service failed");
                r0 = Message.obtain(this.mHandler);
                r0.what = 1003;
                this.mHandler.sendMessage(r0);
            }
        }
    }

    public static OpenFastAppEngine getInstance() {
        return mInstance;
    }

    private void showNormalDialog(Context context) {
        if (context != null) {
            CharSequence string = context.getString(R.string.fastview_sdk_fastapp_noopen_msg);
            Builder builder = new Builder(context);
            builder.setMessage(string);
            builder.setPositiveButton(context.getString(R.string.fastview_sdk_fastapp_aidl_confirm), null);
            this.dialog = builder.create();
            this.dialog.setCanceledOnTouchOutside(false);
            if ((context instanceof Activity) && !((Activity) context).isFinishing() && !((Activity) context).isDestroyed()) {
                this.dialog.show();
            }
        }
    }

    private void unbindService() {
        FastViewLogUtils.e("", "unbind Service");
        try {
            this.mContext.unbindService(this.conn);
        } catch (Exception e) {
            FastViewLogUtils.i("", "unbind service error");
        }
    }

    public void openFastApp(Context context, RpkPageInfo rpkPageInfo, boolean z) {
        if (rpkPageInfo != null && !this.mTimeOut) {
            this.mTimeOut = true;
            this.mNeedDownload = z;
            this.isNeedStartActivity = false;
            FastViewLogUtils.e(TAG, "openFastApp");
            Value value = new Value(context, rpkPageInfo);
            Message obtain = Message.obtain(this.mHandler);
            obtain.what = 1001;
            obtain.obj = value;
            this.mHandler.removeMessages(1001);
            this.mHandler.sendMessage(obtain);
            Message obtain2 = Message.obtain(this.mHandler);
            obtain2.what = 1004;
            this.mHandler.sendMessageDelayed(obtain2, 500);
        }
    }

    public void openFastAppFromDownload(RpkPageInfo rpkPageInfo) {
        if (this.mContext == null) {
            FastViewLogUtils.e(TAG, "(null == mContext");
            return;
        }
        this.isNeedStartActivity = true;
        FastViewLogUtils.e(TAG, "openFastApp first");
        Value value = new Value(this.mContext, rpkPageInfo);
        Message obtain = Message.obtain(this.mHandler);
        obtain.what = 1001;
        obtain.obj = value;
        this.mHandler.sendMessage(obtain);
    }

    public void setJumpActivityStatus(boolean z) {
        FastViewLogUtils.d(TAG, "setActivityStatus: " + z);
        if (z) {
            bindService(this.mContext);
        } else if (this.dialog == null) {
            showNormalDialog(this.mContext);
        } else if (!this.dialog.isShowing()) {
            showNormalDialog(this.mContext);
        }
    }
}
